package cn.cdiothree.controller;


import cn.cdiothree.dataobject.ProductCategory;
import cn.cdiothree.exception.SellException;
import cn.cdiothree.form.CategoryForm;
import cn.cdiothree.service.ProductCategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/seller/category")
public class SellerCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * 类目列表
     * @param map
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(Map<String,Object> map){
        List<ProductCategory> productCategoryList=productCategoryService.findAll();
        map.put("productCategoryList",productCategoryList);
        return new ModelAndView("category/list",map);
    }

    /**
     * 展示
     * @param id
     * @param map
     * @return
     */
    @GetMapping("/index")
    public ModelAndView index(@RequestParam(value = "id",required = false)Integer id,
                              Map<String,Object> map){
        if (id !=null){
            ProductCategory productCategory=productCategoryService.findById(id);
            map.put("category",productCategory);
        }
        return new ModelAndView("category/index",map);
    }

    /**
     * 类目保存/更新
     * @param form
     * @param bindingResult
     * @param map
     * @return
     */
    @PostMapping("/save")
    public  ModelAndView save(@Valid CategoryForm form,
                              BindingResult bindingResult,
                              Map<String,Object> map){
        if (bindingResult.hasErrors()){
            map.put("msg",bindingResult.getFieldError().getDefaultMessage());
            map.put("url","/cumthing/seller/category/index");
            return new ModelAndView("common/error",map);
        }

        ProductCategory productCategory=new ProductCategory();
        try {
            if (form.getId() != null) {
                productCategory = productCategoryService.findById(form.getId());
                BeanUtils.copyProperties(form, productCategory);
                productCategoryService.updateById(productCategory);
            }else {
                BeanUtils.copyProperties(form, productCategory);
                productCategoryService.save(productCategory);
            }
        }catch (SellException e){
            map.put("msg",e.getMessage());
            map.put("url","/cumthing/seller/category/index");
            return new ModelAndView("common/error",map);
        }
        map.put("url","/cumthing/seller/category/list");
        return new ModelAndView("common/success",map);
    }
}
